var app = angular.module('app', []);
app.controller('ctl.login', ['$scope', '$rootScope', '$http',
    function ($scope, $rootScope, $http) {
        $scope.userName = getCookie('USERNAME');
        $scope.pwd = getCookie('SECRETCODE');

        if($scope.pwd){
            document.getElementById('rememberPwd').checked = true;
        }

        $scope.login = function () {
            if ($scope.userName == '' || $scope.pwd == '' || $scope.verifyCode == '') {
                $scope.errorMessage = "请填写完整信息！";
                return;
            }

            if (!isEmpty($scope.pwd) && $scope.pwd != getCookie('SECRETCODE')) {
                $scope.pwd = hex_md5($scope.pwd);
            }

            $http({
                method: 'POST',
                url: baseServices_restfulUrl + 'user/doLogin.json',
                params: {
                    username: $scope.userName,
                    password: $scope.pwd,
                    rememberme:document.getElementById('rememberPwd').checked,
                    verifyCode: $scope.verifyCode
                },
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                }
            }).success(function (resp, header, config, status) {
                if (resp.state == 0) {
                    setCookie('USERNAME', $scope.userName, 7);
                    if(document.getElementById('rememberPwd').checked){
                        setCookie('SECRETCODE', $scope.pwd, 7);
                    }else{
                        removeCookie('SECRETCODE');
                    }

                    setSessionJson('curUserObj',resp.data);
                    location.href = location.origin + webPath;
                } else {
                    $scope.errorMessage = resp.message;
                    $scope.getValidateCode();
                }
            }).error(function (resp, header, config, status) {
                $scope.errorMessage = '程序错误，访问数据失败';
            });
        };

        $scope.getValidateCode = function () {
            document.getElementById('validateCodeImg').setAttribute("src", baseServices_restfulUrl + "common/randomcode/get.json?" + Math.random());
        };

        $scope.getValidateCode();
        
        $scope.goRegister = function () {
        	var url = 'regFirst.html';
        	location.replace(url);
        }
        
        
    }]);
